//
//  ShoppingCarTableViewCell.m
//  AmountBody
//
//  Created by smm on 2017/12/19.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "ShoppingCarTableViewCell.h"
#define Cell_H 160
#define button_Tag 700000
@implementation ShoppingCarTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


-(void)setCellWithModel:(ShoppingCarModel*)model
{
    _model = model;
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 10;
    CGFloat buttonW = 40;
    CGFloat buttinH = buttonW;
    CGFloat lineViewH = 10;
    CGFloat imageH = Cell_H-jianGeH*2-lineViewH;
    CGFloat imageW = imageH;
    
    CGFloat viewH = Cell_H-lineViewH;
    
    _newsView.frame  =CGRectMake(0, 0, SCREEN_W, viewH);
    _chooseButton.frame = CGRectMake(jianGeW-10, (Cell_H-buttinH)/2.0, buttonW, buttinH);
    _chooseButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    if (model.is_Choose) {
        [_chooseButton setImage:[UIImage imageNamed:@"s_choose"] forState:UIControlStateNormal];
    }else{
        [_chooseButton setImage:[UIImage imageNamed:@"s_noChoose"] forState:UIControlStateNormal];
    }
    _headImage.frame = CGRectMake(_chooseButton.frame.origin.x+_chooseButton.frame.size.width+jianGeW-10, jianGeH, imageW, imageH);
    [_headImage sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"r_touxiang"]];
//    _titleLabel.text = model.title;
//    _titleLabel.textColor = ZhongZi_Color;
//    _titleLabel.font = Cell_Big_Font;
    _titleLabel.numberOfLines = 2;
    [_titleLabel sizeToFit];
    
    layoutViews(^{
        _titleLabel.frame = CGRectMake(_headImage.frame.origin.x+_headImage.frame.size.width+jianGeW, jianGeH, 220, 40);
    }, ^{
        _titleLabel.frame = CGRectMake(_headImage.frame.origin.x+_headImage.frame.size.width+jianGeW, jianGeH, 180, 40);
    });
//    _typeLabel.text = model.type;
//    _typeLabel.textAlignment = NSTextAlignmentCenter;
//    _typeLabel.textColor = [UIColor whiteColor];
//    _typeLabel.font = Cell_Big_Font;
//    [_typeLabel sizeToFit];
//    _typeLabel.frame = CGRectMake(_titleLabel.frame.size.width+_titleLabel.frame.origin.x+5, _titleLabel.frame.origin.y+(40-Nomal_Label_H)/2.0, 20, Nomal_Label_H);
//    [_typeLabel setYuanJiaoForRadius:3.0 andMasks:NO];
//    _typeLabel.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    NSString * str = [NSString stringWithFormat:@"%@      ",model.title];
    NSString * str2 = [NSString stringWithFormat:@" %@ ",model.type];
    NSString * needStr = [NSString stringWithFormat:@"%@%@",str,str2];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:needStr];
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset = CGSizeMake(0, 0);      //阴影偏移量
    shadow.shadowColor = ShenLine_color;  //阴影颜色
    shadow.shadowBlurRadius = 3.0;
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:Cell_Big_Font
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:ShenZi_Color
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor whiteColor]
                    range:NSMakeRange(str.length, str2.length)];
    // 添加文字背景颜色
    [attrStr addAttribute:NSBackgroundColorAttributeName
                    value:RGBColorAlpha(44, 44, 44, 1)
                    range:NSMakeRange(str.length, str2.length)];
    //设置圆角
    [attrStr addAttribute:NSShadowAttributeName
                    value:shadow
                    range:NSMakeRange(str.length, str2.length)];
    
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:16.0f]
                    range:NSMakeRange(str.length, str2.length)];
    _titleLabel.attributedText = attrStr;
    _titleLabel.numberOfLines = 2;
    [_titleLabel sizeToFit];
    
    layoutViews(^{
        _titleLabel.frame = CGRectMake(_headImage.frame.origin.x+_headImage.frame.size.width+jianGeW, jianGeH, 260, 40);
    }, ^{
        _titleLabel.frame = CGRectMake(_headImage.frame.origin.x+_headImage.frame.size.width+jianGeW, jianGeH, 180, 40);
    });
    _timeLabel.text = model.time;
    _timeLabel.textColor = QianZi_color;
    _timeLabel.font = Cell_Little_Font;
    [_timeLabel sizeToFit];
    layoutViews(^{
        _timeLabel.frame = CGRectMake(SCREEN_W-jianGeW-_timeLabel.frame.size.width, jianGeH, _timeLabel.frame.size.width, Nomal_Label_H);
    }, ^{
        _timeLabel.frame = CGRectMake(jianGeW*2+_titleLabel.frame.size.width+_titleLabel.frame.origin.x, viewH-Nomal_Label_H-5, _timeLabel.frame.size.width, Nomal_Label_H);
    });
    _newsLabel.text = [NSString stringWithFormat:@"订单号/客户单号：%@    客户名：%@",model.order_number,model.name];
    _newsLabel.textColor = QianZi_color;
    _newsLabel.font = Cell_Little_Font;
    [_newsLabel sizeToFit];
    
    layoutViews(^{
        _newsLabel.frame = CGRectMake(SCREEN_W-jianGeW*2-_newsLabel.frame.size.width-_timeLabel.frame.size.width, jianGeH, _newsLabel.frame.size.width, Nomal_Label_H);
        
    }, ^{
        _newsLabel.frame = CGRectMake(SCREEN_W-jianGeW-_newsLabel.frame.size.width, jianGeH, _newsLabel.frame.size.width, Nomal_Label_H);
    });
    _priceLabel.text = [NSString stringWithFormat:@"¥%@",model.price];
    _priceLabel.textColor = RGBColorAlpha(115, 206, 15, 1);
    _priceLabel.font = [UIFont systemFontOfSize:18];;
    [_priceLabel sizeToFit];
    
    layoutViews(^{
        _priceLabel.frame = CGRectMake(SCREEN_W-jianGeW*4-_priceLabel.frame.size.width, _newsLabel.frame.size.height+_newsLabel.frame.origin.y+25, _priceLabel.frame.size.width, Nomal_Label_H);
    }, ^{
        _priceLabel.frame = CGRectMake(SCREEN_W-jianGeW*4-_priceLabel.frame.size.width, _newsLabel.frame.size.height+_newsLabel.frame.origin.y+15, _priceLabel.frame.size.width, Nomal_Label_H);
    });
    CGFloat m_buttonW = 40;
    CGFloat m_buttonH = m_buttonW;
    _moreButton.frame = CGRectMake(SCREEN_W-jianGeW-m_buttonW, _priceLabel.frame.origin.y+_priceLabel.frame.size.height+10, m_buttonW, m_buttonH);
    _moreButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    CGFloat n_viewW = 200;
    CGFloat n_viewH = 40;
    _numberView.frame = CGRectMake(SCREEN_W-jianGeW-m_buttonW-n_viewW-jianGeW/2.0, _moreButton.frame.origin.y, n_viewW, n_viewH);
    if (_model.isTuoGuan) {
        
        _n_namelabel.text = @"托管中";
    }else{
       
        _n_namelabel.text = @"保存中";
    }
    _n_namelabel.textColor = QianZi_color;
    _n_namelabel.font = Cell_Medium_Font;
    [_n_namelabel sizeToFit];
    _n_namelabel.frame = CGRectMake(0, (n_viewH-Nomal_Label_H)/2.0, _n_namelabel.frame.size.width, Nomal_Label_H);
    _n_view.frame  =CGRectMake(_n_namelabel.frame.size.width+_n_namelabel.frame.origin.x+20, 0, n_viewW-(_n_namelabel.frame.size.width+_n_namelabel.frame.origin.x+20), n_viewH);
    CGFloat l_viewW =(n_viewW-(_n_namelabel.frame.size.width+_n_namelabel.frame.origin.x+20)-20)/3.0;
    [_n_view setBianKuangAndColor:RGBColorAlpha(235, 235, 235, 1)];
    [_n_view setYuanJiaoForRadius:5.0 andMasks:NO];
    _n_cutButton.frame = CGRectMake(0, 0, l_viewW, n_viewH);
    [_n_cutButton setTitle:@"-" forState:UIControlStateNormal];
    [_n_cutButton setTitleColor:QianZi_color forState:UIControlStateNormal];
    _n_text.frame = CGRectMake(_n_cutButton.frame.origin.x+_n_cutButton.frame.size.width, 0, l_viewW+20, n_viewH);
    _n_text.text =@"1";
    _n_text.textColor = ShenZi_Color;
    _n_text.textAlignment = NSTextAlignmentCenter;
    [_n_text borderForColor:RGBColorAlpha(235, 235, 235, 1) borderWidth:1 borderType:UIBorderSideTypeLeft | UIBorderSideTypeRight];
    _n_addButton.frame = CGRectMake(_n_text.frame.origin.x+_n_text.frame.size.width, 0, l_viewW, n_viewH);
    [_n_addButton setTitle:@"+" forState:UIControlStateNormal];
    [_n_addButton setTitleColor:ShenZi_Color forState:UIControlStateNormal];
    _numberLabel.textColor = QianZi_color;
    _numberLabel.font = Cell_Little_Font;
    _numberLabel.text = model.number;
    [_numberLabel sizeToFit];
    _numberLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y+_titleLabel.frame.size.height, _numberLabel.frame.size.width, Nomal_Label_H);
    
    CGFloat a_viewH = 30;
    CGFloat a_imageW = 35;
    CGFloat a_imageH = a_imageW;
    _addView.frame = CGRectMake(_numberLabel.frame.origin.x, _numberLabel.frame.origin.y+_numberLabel.frame.size.height+10, SCREEN_W/2.0-(_numberLabel.frame.origin.x), a_viewH);
    CGFloat l_jianGeW = 10;
    _a_chooseButton.frame = CGRectMake(0, (a_viewH-a_imageH)/2.0, a_imageW, a_imageH);
    _a_chooseButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    _a_label.text = @"追加一条西裤";
    _a_label.textColor = QianZi_color;
    _a_label.font = Cell_Little_Font;
    [_a_label sizeToFit];
    _a_label.frame = CGRectMake(_a_chooseButton.frame.origin.x+_a_chooseButton.frame.size.width, (a_viewH-Nomal_Label_H)/2.0, _a_label.frame.size.width, Nomal_Label_H);
    _addView.frame = CGRectMake(_numberLabel.frame.origin.x, _numberLabel.frame.origin.y+_numberLabel.frame.size.height+10, _a_label.frame.origin.x+_a_label.frame.size.width+jianGeW, a_viewH);
    if (model.hasAdd) {
        _addView.hidden = NO;
    }else{
        _addView.hidden = YES;
    }
    CGFloat t_viewH = 30;
   
    _tagView.frame = CGRectMake(_numberLabel.frame.origin.x, viewH-t_viewH-5, 200, t_viewH);
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tagViewFunc:)];
    [_tagView addGestureRecognizer:tap];
    
    CGFloat x_imageW = 12;
    CGFloat x_imageH = x_imageW;
    _t_xingImage.frame = CGRectMake(l_jianGeW, (t_viewH-x_imageH)/2.0, x_imageW, x_imageH);
    _t_xingImage.image = [UIImage imageNamed:@"Star"];
    
    _t_label.text = model.tagName;
    _t_label.textColor = [UIColor whiteColor];
    _t_label.font = Cell_Little_Font;
    [_t_label sizeToFit];
    _t_label.frame = CGRectMake(_t_xingImage.frame.origin.x+_t_xingImage.frame.size.width+l_jianGeW, (t_viewH-Nomal_Label_H)/2.0, _t_label.frame.size.width, Nomal_Label_H);
    
    _tagView.frame = CGRectMake(_numberLabel.frame.origin.x, viewH-t_viewH-5, _t_label.frame.origin.x+_t_label.frame.size.width+l_jianGeW*2, t_viewH);
    _tagView.image = [self imageWithColor:model.tagColor];
    [_tagView bringSubviewToFront:_t_label];
    [_tagView bringSubviewToFront:_t_xingImage];
    _lineView.frame  =CGRectMake(0, Cell_H-lineViewH, SCREEN_W, lineViewH);
    _lineView.backgroundColor = RGBColorAlpha(240, 240, 240, 1);
    [_lineView setBianKuangAndColor:RGBColorAlpha(235, 235, 235, 1)];
    [self loadTanchuView];
}

-(void)loadTanchuView
{
    _tanchuView.hidden = YES;
    for (UIView * view in _tanchuImage.subviews) {
        [view removeFromSuperview];
    }
    CGFloat jianGeW = 10;
    CGFloat imageW = 608;
    CGFloat imageH = 69;
    CGFloat buttonW = (imageW-6)/6.0;
    CGFloat buttonH = imageH-15;
    _tanchuView.frame = CGRectMake(SCREEN_W-jianGeW, _moreButton.frame.origin.y-imageH, 0, imageH);
    _tanchuView.clipsToBounds = YES;
    _tanchuImage.frame = CGRectMake(0, 0, imageW, imageH);
    _tanchuImage.image = [UIImage imageNamed:@"s_backRectangle"];
    [_newsView bringSubviewToFront:_tanchuView];
    NSArray * nameArray ;
    if (_model.isTuoGuan) {
        nameArray = [NSArray arrayWithObjects:@"取消托管",@"MTO",@"预提交",@"编辑",@"再次购买",@"删除", nil];
    }else{
         nameArray = [NSArray arrayWithObjects:@"托管",@"MTO",@"预提交",@"编辑",@"再次购买",@"删除", nil];
    }
    
    NSArray * tagArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:ButtonTypeForTG],[NSNumber numberWithInt:ButtonTypeForMTO],[NSNumber numberWithInt:ButtonTypeForYTJ],[NSNumber numberWithInt:ButtonTypeForBJ],[NSNumber numberWithInt:ButtonTypeForZCGM],[NSNumber numberWithInt:ButtonTypeForSC],nil];
    NSArray * imageArray = [NSArray arrayWithObjects:@"s_tuoguan",@"MTOicon",@"s_yutijiao",@"s_edit",@"s_againShop",@"s_delegate", nil];
    for (int i=0; i<nameArray.count; i++) {
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0+(1+buttonW)*i, 0, buttonW, buttonH)];
        button.tag = button_Tag+[[tagArray objectAtIndex:i] intValue];
        [button setTitle:[nameArray objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:ZhongZi_Color forState:UIControlStateNormal];
        button.titleLabel.font = Cell_Medium_Font;
        [button setImage:[UIImage imageNamed:[imageArray objectAtIndex:i]] forState:UIControlStateNormal];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [button addTarget:self action:@selector(buttinFunc:) forControlEvents:UIControlEventTouchUpInside];
        //imageEdgeInsets和titleEdgeInsets
        [_tanchuImage addSubview:button];
        _tanchuImage.userInteractionEnabled = YES;
        if (i<nameArray.count-1) {
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(buttonW+(1+buttonW)*i, 10, 1, buttonH-20)];
            lineView.backgroundColor = RGBColorAlpha(235, 235, 235, 1);
            [_tanchuImage addSubview:lineView];
        }
        
    }
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark - 颜色渲染图片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); //宽高 1.0只要有值就够了
    UIGraphicsBeginImageContext(rect.size); //在这个范围内开启一段上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);//在这段上下文中获取到颜色UIColor
    CGContextFillRect(context, rect);//用这个颜色填充这个上下文
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//从这段上下文中获取Image属性,,,结束
    UIGraphicsEndImageContext();
    
    return image;
}


#pragma mark - 手势按钮方法
-(void)buttinFunc:(UIButton *)button
{
    ButtonType buttonType = (int)button.tag-button_Tag;
    if (buttonType == ButtonTypeForTG) {
        _model.isTuoGuan = !_model.isTuoGuan;
        if (_model.isTuoGuan) {
            [button setTitle:@"取消托管" forState:UIControlStateNormal];
            _n_namelabel.text = @"托管中";
        }else{
            [button setTitle:@"托管" forState:UIControlStateNormal];
            _n_namelabel.text = @"保存中";
        }
        
    }
    if (_typeBlock) {
        _typeBlock(buttonType);
    }
}
-(void)tagViewFunc:(UITapGestureRecognizer*)tap
{
    if (_colorBlock) {
        _colorBlock(_model);
    }
}
- (IBAction)chooseButton:(UIButton *)sender {
    _model.is_Choose = !_model.is_Choose;
    if (_model.is_Choose) {
        [_chooseButton setImage:[UIImage imageNamed:@"s_choose"] forState:UIControlStateNormal];
    }else{
        [_chooseButton setImage:[UIImage imageNamed:@"s_noChoose"] forState:UIControlStateNormal];
    }
}
- (IBAction)a_chooseButton:(UIButton *)sender {
    _model.is_aChoose = !_model.is_aChoose;
    if (_model.is_aChoose) {
        [_a_chooseButton setImage:[UIImage imageNamed:@"s_zchoose"] forState:UIControlStateNormal];
    }else{
        [_a_chooseButton setImage:[UIImage imageNamed:@"s_znoChoose"] forState:UIControlStateNormal];
    }
}
- (IBAction)n_addButton:(UIButton *)sender {
    int index = [_n_text.text intValue];
    index++;
    _n_text.text = [NSString stringWithFormat:@"%d",index];
}

- (IBAction)n_cutButton:(UIButton *)sender {
    int index = [_n_text.text intValue];
    index--;
    if (index<1) {
        return;
    }
    _n_text.text = [NSString stringWithFormat:@"%d",index];
}
- (IBAction)moreButton:(UIButton *)sender {
    _isTanchu = !_isTanchu;
    if (_isTanchu) {
        _tanchuView.hidden = NO;
        [UIView animateWithDuration:0.37 animations:^{
            CGFloat jianGeW = 10;
            CGFloat imageW = 608;
            CGFloat imageH = 69;
            _tanchuView.frame = CGRectMake(SCREEN_W-jianGeW-imageW, _moreButton.frame.origin.y-imageH, imageW, imageH);
        }];
        [_moreButton setImage:[UIImage imageNamed:@"a_gengduo"] forState:UIControlStateNormal];
    }else{
        [UIView animateWithDuration:0.37 animations:^{
            CGFloat jianGeW = 10;
            CGFloat imageW = 608;
            CGFloat imageH = 69;
            _tanchuView.frame = CGRectMake(SCREEN_W-jianGeW, _moreButton.frame.origin.y-imageH, 0, imageH);
        } completion:^(BOOL finished) {
            _tanchuView.hidden = YES;
            [_moreButton setImage:[UIImage imageNamed:@"gengduo"] forState:UIControlStateNormal];
        }];
        
    }
}

#pragma mark - Block
static ColorChooseBlock _colorBlock;

+(void)blockForColorChooseView:(ColorChooseBlock)colorBlock
{
    _colorBlock = colorBlock;
}

static ButtonTypeBlock _typeBlock;
+(void)blockForButtonType:(ButtonTypeBlock)typeBlock
{
    _typeBlock = typeBlock;
}

@end
